package com.example.web_chat.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket //启动websocket
public class WebSocketConfig implements WebSocketConfigurer {
//    @Autowired
//    private TestWebSocketAPI testWebSocketAPI;

    @Autowired
    private WebSocketAPI webSocketAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //将TestWebSocketAPI 这个类注册到具体的路径上
        //此时，当浏览器的websocket请求路径是/test时，就会自动调用testWebSocketAPI这个类中的方法
        //请求路径和testWebSocket进行一个关联，后续访问这个路径时，才能调用里面的方法
//        registry.addHandler(testWebSocketAPI, "/test");

        registry.addHandler(webSocketAPI, "/message")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        //注入一个特殊的拦截器，就可以把用户给httpSession中添加的attribute键值对在websocketSession中也添加一份


    }
}
